package com.sharefang.aproject.utils;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.provider.Settings;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;

import com.balysv.materialripple.MaterialRippleLayout;
import com.gc.materialdesign.widgets.SnackBar;
import com.sharefang.aproject.R;
import com.sharefang.aproject.activity.BaseActivity;
import com.sharefang.aproject.activity.LoginActivity;

import java.util.Timer;
import java.util.TimerTask;

import cn.pedant.SweetAlert.SweetAlertDialog;


/**
 * Created by Administrator on 2016/7/26.
 */

public class UiUtil {
    private UiUtil() throws Exception {
        throw new Exception("不能创建本类的实例");
    }

    private static Boolean isExit = false;

    public synchronized static void doubleClickToFinishApp(Context context) {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            showToast(context, "再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            BaseActivity.exitApp();
        }
    }

    /**
     * 防止按钮的连续点击
     */
    public static final long INTERVAL = 500L;
    private static long lastClickTime = 0L;

    public synchronized static boolean isFastClick() {
        if (lastClickTime == 0) {
            lastClickTime = System.currentTimeMillis();
            return false;
        }
        long time = System.currentTimeMillis();

        if (time - lastClickTime > INTERVAL) {
            lastClickTime = time;
            return false;
        }
        lastClickTime = time;
        return true;
    }

    public static void showToast(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    static SnackBar snackBar = null;

    public static void snake(Activity activity, String message) {
        snackBar = new SnackBar(activity, message);

        snackBar.setDismissTimer(2000);
        snackBar.show();

    }

    public static void dismissSnake() {
        if (snackBar != null) {
            snackBar.dismiss();
        }
    }

    /**
     * 监听软键盘状态，并在软键盘状态改变时回调
     *
     * @param activity
     * @param listener
     */
    public static void observeSoftKeyboard(Activity activity, final OnSoftKeyboardChangeListener listener) {
        final View decorView = activity.getWindow().getDecorView();
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            int previousKeyboardHeight = -1;

            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                decorView.getWindowVisibleDisplayFrame(rect);
                int displayHeight = rect.bottom - rect.top;
                int height = decorView.getHeight();
                int keyboardHeight = height - displayHeight;
                if (previousKeyboardHeight != keyboardHeight) {
                    boolean hide = (double) displayHeight / height > 0.8;
                    listener.onSoftKeyBoardChange(keyboardHeight, !hide);
                }

                previousKeyboardHeight = height;

            }
        });
    }

    public interface OnSoftKeyboardChangeListener {
        void onSoftKeyBoardChange(int softKeybardHeight, boolean visible);
    }

    /**
     * 纵向滑动View，以自适应软键盘出现
     *
     * @param view
     * @param action
     * @param startY
     * @param toY
     */
    public static void slideView(View view, String action, float startY, float toY) {
        ObjectAnimator.ofFloat(view, action, startY, toY).setDuration(500).start();
    }

    /**
     * View的位移动画，并在动画完成后改变View的位置
     *
     * @param view
     * @param xFrom
     * @param xTo
     * @param yFrom
     * @param yTo
     */
    public static void slideView(final View view, final float xFrom, final float xTo, final float yFrom, final float yTo) {
        TranslateAnimation animation = new TranslateAnimation(xFrom, xTo, yFrom, yTo);
        //添加了这行代码的作用时，view移动的时候 会有弹性效果
        animation.setInterpolator(new OvershootInterpolator());
        animation.setDuration(500);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                int left = view.getLeft() + (int) (xTo - xFrom);
                int top = view.getTop() + (int) (yTo - yFrom);
                int width = view.getWidth();
                int height = view.getHeight();
                view.clearAnimation();
                view.layout(left, top, left + width, top + height);
            }
        });
        view.startAnimation(animation);
    }

    static SweetAlertDialog dialog = null;

    public synchronized static void showLoadingDialog(Context context) {
        dialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
        dialog.getProgressHelper().setBarColor(context.getResources().getColor(R.color.main_color_green));
        dialog.setTitleText("");
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    public synchronized static void showLoadingDialog(Context context, String title) {
        dialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
        dialog.getProgressHelper().setBarColor(context.getResources().getColor(R.color.main_color_green));
        dialog.setTitleText(title);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    public synchronized static void showLoadingDialog(Context context, String title, int progressColor) {
        dialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
        dialog.getProgressHelper().setBarColor(progressColor);
        dialog.setTitleText(title);

        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    public synchronized static void hideLoadingDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
            System.gc();
        }
    }
}
